package backend

import "encoding/json"

type Message struct {
	Op        string `json:"op"`
	Status    int    `json:"status"`
	Msg       string `json:"message,omitempty"`
	RpcId     int    `json:"rpc-id,omitempty"`
	Url       string `json:"url,omitempty"`
	BackendId int    `json:"backend-id,omitempty"`
	RtcId     string `json:"rtc-id,omitempty"`
	ConfId    string `json:"conf-id,omitempty"`
	//RtcIdList []string `json:"rtc-id-list,omitempty"`
	ConfList []Conf `json:"conf-list,omitempty"`
}

type Request struct {
	Op string `json:"op"`
}

type Response struct {
	Op string `json:"op"`
}

type Conf struct {
	Id      string   `json:"id,omitempty"`
	Kind    string   `json:"kind,omitempty"`
	RtcList []string `json:"rtc-list,omitempty"`
}

const (
	OpResponse   = "response"
	OpRegister   = "register"
	OpUnRegister = "un-register"
	OpKeepAlive  = "keepalive"
	OpRtcDestroy = "rtc-destroy"
)

func ParseMessage(buf []byte) (*Message, error) {
	msg := &Message{}
	if err := json.Unmarshal(buf, msg); err != nil {
		log.Warningf("could not parse message: %s", buf)
		return nil, err
	}
	return msg, nil
}
